﻿Imports BUS
Imports DAO
Imports DTO
Imports System.Linq
Imports System.Text.RegularExpressions

Public Class DanhSachHS

    Private Sub cbx_khoi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_khoi.SelectedIndexChanged
        lbl_k.Text = ""
        Dim temp As Integer
        Dim k As KhoiDTO = cbx_khoi.SelectedValue
        temp = k.MaKhoi

        Dim lop As LopBUS = New LopBUS()
        cbx_lop.DisplayMember = "TenLop"
        cbx_lop.ValueMember = "MaL"

        If temp = 0 Then
            Dim list As List(Of LopDTO) = New List(Of LopDTO)
            Dim l3 As New LopDTO
            l3.MaLop = ""
            l3.TenLop = "--- Tất cả ---"

            list.Add(l3)

            For Each k2 As KhoiDTO In cbx_khoi.Items
                If k2.MaKhoi <> 0 Then
                    Dim list2 As List(Of LopDTO) = lop.getClass_BUS(k2.MaKhoi)
                    For Each l As LopDTO In list2
                        list.Add(l)
                    Next
                End If
            Next

            cbx_lop.DataSource = list
        Else
            cbx_lop.DataSource = lop.getClass_BUS(temp)
        End If
    End Sub

    Private Sub cbx_year_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lbl_error.Text = ""
    End Sub

    Private Sub cbx_lop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_lop.SelectedIndexChanged
        lbl_error.Text = ""

        'Dim temp As String
        'Dim lop As LopDTO = cbx_lop.SelectedValue
        'temp = lop.MaLop

        'Dim khoi As KhoiBUS = New KhoiBUS()
        'cbx_khoi.DisplayMember = "TenKhoi"
        'cbx_khoi.ValueMember = "MaK"

        'cbx_khoi.DataSource = khoi.getKhoitheoLop_BUS(temp)
    End Sub

    Private Sub CapNhatHS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        date_date.Format = DateTimePickerFormat.Custom
        date_date.CustomFormat = "dd/MM/yyyy"

        Dim kBus As KhoiBUS = New KhoiBUS()
        cbx_khoi.DisplayMember = "TenKhoi"
        cbx_khoi.ValueMember = "MaK"

        Dim list As List(Of KhoiDTO) = kBus.getLevel_BUS()
        Dim khoidto As New KhoiDTO
        khoidto.MaKhoi = 0
        khoidto.TenKhoi = "--- Tất cả ---"

        list.Insert(0, khoidto)

        cbx_khoi.DataSource = list

        Dim temp As Integer
        Dim k As KhoiDTO = cbx_khoi.SelectedValue
        temp = k.MaKhoi

        Dim yBUS As HocKyBUS = New HocKyBUS()
        cbx_year.DisplayMember = "NamHoc"
        cbx_year.ValueMember = "NamHoc"
        cbx_year.DataSource = yBUS.getYear_BUS()

        Dim ttBus As TinhTrangBUS = New TinhTrangBUS()
        cbx_tinhtrang.DisplayMember = "LoaiTT"
        cbx_tinhtrang.ValueMember = "MaTT"

        Dim listtt As List(Of TinhTrangDTO) = ttBus.getTinhTrang_BUS()
        Dim ttDto As New TinhTrangDTO
        ttDto.MaTT = 0
        ttDto.LoaiTT = "--- Tất cả ---"
        listtt.Insert(0, ttDto)
        cbx_tinhtrang.DataSource = listtt

        'load ds hoc sinh
        Dim hsBus As HoSoHSBUS = New HoSoHSBUS()
        dgv_list.DataSource = hsBus.getProfile_BUS()
        Dim stt As Integer = 0
        For Each row As DataGridViewRow In dgv_list.Rows
            row.Cells(0).Value = stt + 1
            stt = stt + 1
        Next
    End Sub

    Private Sub txt_name_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_name.KeyPress
        lbl_error.Text = ""
        lbl_name.Text = ""
    End Sub

    Public Shared Function IsEmail(ByVal Email As String) As Boolean
        Dim strEmail As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" & "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" & ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

        Dim re As New Regex(strEmail)
        If re.IsMatch(Email) Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    Public Function IsBorn(ByVal born As Date) As Boolean
        If ((Date.Today.Year - date_date.Value.Date.Year > 20) Or (Date.Today.Year - date_date.Value.Date.Year < 15)) Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_update.Click
        Dim kDto As KhoiDTO = cbx_khoi.SelectedValue
        Dim mak As Integer = kDto.MaKhoi

        Dim lDto As LopDTO = cbx_lop.SelectedValue
        Dim mal As String = lDto.MaLop

        Dim matt As Integer = cbx_tinhtrang.SelectedValue
        Dim stname As String = txt_name.Text
        Dim address As String = txt_address.Text
        Dim email As String = txt_email.Text
        Dim borndate As Date = date_date.Value
        Dim mahs As Integer = Me.dgv_list.Item(11, dgv_list.CurrentRow.Index).Value.ToString()
        Dim sex As Integer
        If rd_nam.Checked = True Then
            sex = 1
        Else
            sex = 2
        End If
        If (txt_name.Text.Trim() = "") Then
            lbl_name.Text = "Tên không được trống!!!"
            txt_name.Focus()
        ElseIf (txt_address.Text.Trim() = "") Then
            lbl_address.Text = "Thông tin bắt buộc"
            txt_address.Focus()
        ElseIf (txt_email.Text.Trim() = "") Then
            lbl_email.Text = "Thông tin bắt buộc"
            txt_email.Focus()
        ElseIf (Me.IsBorn(date_date.Value) = False) Then
            lbl_date.Text = "Học sinh không đủ tuổi quy định"
            date_date.Focus()
        ElseIf rd_nam.Checked = False And rd_nu.Checked = False Then
            lbl_sex.Text = "Vui lòng chọn giới tính"
        ElseIf mak = 0 Then
            lbl_k.Text = "Vui lòng chọn khối!!!"
        ElseIf mal = "" Then
            lbl_l.Text = "Vui lòng chọn lớp!!!"
        ElseIf matt = 0 Then
            lbl_tt.Text = "Vui lòng chọn tình trạng!!!"
        Else
            Dim mahoso As Integer = Me.dgv_list.Item(1, dgv_list.CurrentRow.Index).Value.ToString()
            Dim hsname As String = txt_name.Text

            Dim hosoBus As HoSoHSBUS = New HoSoHSBUS()
            hosoBus.updateProfileStudent_BUS(mahoso, mak, mal, matt)

            Dim hsBus As HocSinhBUS = New HocSinhBUS()
            hsBus.UpdateStudent_BUS(mahs, stname, address, email, borndate, sex)

            lbl_error.Text = "Cập nhật thành công"

            dgv_list.DataSource = hosoBus.getProfile_BUS()

            Dim stt2 As Integer = 0
            For Each row As DataGridViewRow In dgv_list.Rows
                row.Cells(0).Value = stt2 + 1
                stt2 = stt2 + 1
            Next
        End If

    End Sub

    Private Sub dgv_list_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_list.SelectionChanged
        txt_name.Text = Me.dgv_list.Item(2, dgv_list.CurrentRow.Index).Value.ToString()
        date_date.Value = Me.dgv_list.Item(3, dgv_list.CurrentRow.Index).Value.ToString()
        txt_address.Text = Me.dgv_list.Item(5, dgv_list.CurrentRow.Index).Value.ToString()
        txt_email.Text = Me.dgv_list.Item(6, dgv_list.CurrentRow.Index).Value.ToString()
        Dim temp = Me.dgv_list.Item(4, dgv_list.CurrentRow.Index).Value.ToString()
        If temp = "Nam" Then
            rd_nam.Checked = True
        Else
            rd_nu.Checked = True
        End If
        cbx_lop.Text = Me.dgv_list.Item(7, dgv_list.CurrentRow.Index).Value.ToString()
        cbx_tinhtrang.Text = Me.dgv_list.Item(10, dgv_list.CurrentRow.Index).Value.ToString()
        cbx_year.Text = Me.dgv_list.Item(12, dgv_list.CurrentRow.Index).Value.ToString()

        Dim id As Integer = Me.dgv_list.Item(13, dgv_list.CurrentRow.Index).Value.ToString()
        Dim lvBus As KhoiBUS = New KhoiBUS()
        cbx_khoi.Text = lvBus.getLevelFollowId_BUS(id)

    End Sub

    Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
        Close()
    End Sub

    Private Sub btn_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_find.Click
        Dim kDto As KhoiDTO = cbx_khoi.SelectedValue
        Dim mak As Integer = kDto.MaKhoi

        Dim lDto As LopDTO = cbx_lop.SelectedValue
        Dim mal As String = lDto.MaLop

        Dim matt As Integer = cbx_tinhtrang.SelectedValue
        Dim stname As String = txt_name.Text
        Dim address As String = txt_address.Text
        Dim email As String = txt_email.Text
        Dim borndate As Date = date_date.Value
        Dim sex As Integer
        If rd_nam.Checked = True Then
            sex = 1
        ElseIf rd_nu.Checked = True Then
            sex = 2
        Else
            sex = 0
        End If
        Dim year As Integer = cbx_year.SelectedValue

        Dim pfBus As HoSoHSBUS = New HoSoHSBUS()
        dgv_list.DataSource = pfBus.findStudent_BUS(stname, address, email, borndate, sex, mak, mal, year, matt)

        Dim stt1 As Integer = 0
        For Each row As DataGridViewRow In dgv_list.Rows
            row.Cells(0).Value = stt1 + 1
            stt1 = stt1 + 1
        Next

        If Me.dgv_list.RowCount = 0 Then
            txt_name.Text = stname
            txt_email.Text = email
            txt_address.Text = address
            date_date.Value = Date.Today()
            lbl_error.Text = "Không tìm thấy học sinh"
        Else
            lbl_error.Text = ""
        End If
    End Sub

    'Private Sub txt_name_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_name.Leave
    'If (txt_name.Text.Trim() = "") Then
    '    lbl_name.Text = "Tên không được trống!!!"
    '    txt_name.Focus()
    'End If
    'End Sub

    'Private Sub txt_address_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_address.Leave
    '    If (txt_address.Text.Trim() = "") Then
    '        lbl_address.Text = "Thông tin bắt buộc"
    '        txt_address.Focus()
    '    End If
    'End Sub

    Private Sub txt_email_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_email.Leave
        If (txt_email.Text.Trim() = "") Then
            lbl_email.Text = "Thông tin bắt buộc"
            txt_email.Focus()
        ElseIf (IsEmail(txt_email.Text.Trim()) = False) Then
            lbl_email.Text = "Email không hợp lệ"
            txt_email.Focus()
            Return
        Else
            lbl_email.Text = "Email hợp lệ"
        End If
    End Sub


    'Private Sub date_date_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles date_date.Leave
    '    If (Me.IsBorn(date_date.Value) = False) Then
    '        lbl_date.Text = "Học sinh phải từ 15 tới 20 tuổi"
    '        date_date.Focus()
    '    End If
    'End Sub

    Private Sub txt_address_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_address.KeyPress
        lbl_address.Text = ""
    End Sub

    Private Sub txt_email_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_email.KeyPress
        lbl_email.Text = ""
    End Sub

    Private Sub date_date_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles date_date.KeyPress
        lbl_date.Text = ""
    End Sub

    Private Sub cbx_tinhtrang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_tinhtrang.SelectedIndexChanged
        lbl_tt.Text = ""
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_email.Text = ""
        txt_address.Text = ""
        txt_name.Text = ""
        date_date.Value = Date.Today()
        lbl_error.Text = ""
    End Sub

    Private Sub rd_nam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rd_nam.CheckedChanged
        lbl_sex.Text = ""
    End Sub

    Private Sub rd_nu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rd_nu.CheckedChanged
        lbl_sex.Text = ""
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub dgv_list_RowHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_list.RowHeaderMouseClick
        Return
    End Sub

    Private Sub dgv_list_ColumnHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_list.ColumnHeaderMouseClick
        Dim col As DataGridViewColumn
        For Each col In dgv_list.Columns
            col.SortMode = DataGridViewColumnSortMode.NotSortable
        Next

    End Sub
End Class